<template>
    <view>
        <view class="getCash">
            <view class="choose_bank_box" @tap="goToCard">
                <view class="choose_text">选择银行卡</view>
                <view class="bank_info_box">
                    <view class="bank_logo_box">
                        <view><image class="bank_logo" :src="imgUrl+'static/img/'+bank_card_info.bank_logo" /></view>
                        <view class="bank_name">{{bank_card_info.name}}</view>
                    </view>
                    <view class="bank_num">{{bank_card_info.bank_number}}</view>
                </view>
                <view class="arrow_box"><image  class="arrow_img"  :src="imgUrl+'/static/img/jiantou.png'"/></view>

            </view>
            <view class="cash_box_1" @tap="getCashList()">
                <view></view>
                <view class="cash_box_1_1">
                    <view class="small_class_text">提现记录</view>
                    <view class="small_img_box"><image class="small_img" src="../../static/img/small_que.png"/></view>
                </view>

            </view>
            <view class="cash_box_2">
                <view class="money_img">￥</view>
                <view><input class="money_num" type="digit" @input="handleMoney"  :maxlength="maxlength" v-model="cashMoney" placeholder="0" /></view>
            </view>
            <view class="cash_box_3">
                <view class="vis_money">可提现金额 ￥{{has_money}}</view>
                <view class="all_money" @tap="allGetCash()">全部提现</view>
            </view>
            <view class="cash_box_3">
                <view class="vis_money">手机号：{{mobile}}</view>
            </view>
            <view class="cash_box_3">
                <view>
                    <input class="money_num_1" type="number" maxlength="4"  v-model="code" placeholder="请输入验证码" />
                </view>
                <view class="all_money_1"><button class="f-btn" :disabled="btnDis" @tap="handleSendCode()">{{btnText}}</button></view>
            </view>
        </view>
        <view class="tixian_text">提现时间为：工作日 9:00 - 15:00</view>
        <view class="cash_button_box" @tap="queryCash()"><button class="cash_button">确定提现</button></view>
    </view>

</template>

<script>
    export default {
        name: "getCash",
        data() {
            return {
                cashMoney:'',
                has_money:'',
                imgUrl:this.$mConfig.assetsPath,
                bank_card_info:[],
                bank_card_id:'',
                code:'',
                mobile:'',
                btnText: "发送验证码",
                t: null,
                btnDis: false,
                can_send:true,
                maxlength:10
            }
        },
        onLoad(){},
        onShow(){
            this.bank_card_id=this.$store.state.chooseBankCardId
            this.getMyBankCardInfo()
            this.getUserInfo()
        },
        methods: {
            countDown() {
                this.t = null;
                let timer = 60;
                this.btnDis = true;
                clearInterval(this.t);
                this.btnText = `剩余${timer}s`;
                this.t = setInterval(() => {
                    if (timer == 0) {
                        clearInterval(this.t);
                        this.btnText = "重新发送";
                        this.btnDis = false;
                        return;
                    }
                    timer--;
                    this.btnText = `剩余${timer}s`;
                }, 1000)
            },
            async handleSendCode() {
                let reg=/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/
                if(this.cashMoney===''){
                    uni.showToast({
                        title:"请输入正确的提现金额",
                        icon: 'none'
                    })
                    return
                }
                if(!reg.test(this.cashMoney)){
                    uni.showToast({
                        title:"请输入正确的提现金额",
                        icon: 'none'
                    })
                    return
                }
                if(parseFloat(this.cashMoney)<100){
                    uni.showToast({
                        title:"请输入大于100的提现金额",
                        icon: 'none'
                    })
                    return
                }
                if(!this.mobile){
                    uni.showToast({
                        title:"发送验证码的手机号有误",
                        icon: 'none'
                    })
                    return
                }
                if(!this.can_send){
                    uni.showToast({
                        title:"请输入最多含有两位小数的提现金额",
                        icon: 'none'
                    })
                    return
                }
                uni.showLoading({})
                let resData = await this.$apis.getMobileCode({mobile: this.mobile})
                if (resData.code === 0) {
                    this.countDown()
                }
                uni.hideLoading()
            },
            async getMyBankCardInfo(){
                let resData = await this.$apis.getMyBankCardList({access_token:'',id:this.bank_card_id})
                if(resData.code===0){
                    this.bank_card_info=resData.data.lists[0]
                    if(!this.bank_card_id){
                        this.bank_card_id=this.bank_card_info.id
                    }
                }
            },
            allGetCash(){
                this.cashMoney=this.has_money
            },
            async queryCash(){ //确认提现
                let reg=/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/
                if(this.cashMoney===''){
                    uni.showToast({
                        title:"请输入正确的提现金额",
                        icon: 'none'
                    })
                    return
                }
                if(!reg.test(this.cashMoney)){
                    uni.showToast({
                        title:"请输入正确的提现金额",
                        icon: 'none'
                    })
                    return
                }
                if(parseFloat(this.cashMoney)<100){
                    uni.showToast({
                        title:"请输入大于100的提现金额",
                        icon: 'none'
                    })
                    return
                }
                if(!this.code){
                    uni.showToast({
                        title:"请输入验证码",
                        icon: 'none'
                    })
                    return
                }
                if(!this.bank_card_id){
                    uni.showToast({
                        title:"请选择提现到账的银行卡",
                        icon: 'none',
                        duration:2000
                    })
                    return
                }
                if(parseFloat(this.cashMoney)>parseFloat(this.has_money)){
                    uni.showToast({
                        title:"提现金额超过最大可提现金额",
                        icon: 'none',
                        duration:2000
                    })
                    return
                }
                let resData = await  this.$apis.getCash({price:this.cashMoney,code:this.code,bank_id:this.bank_card_id,small_type:1})
                if(resData.code===0){
                    uni.showToast({
                        title:"提交成功",
                        icon: 'success'
                    })
                    let user_data = await this.$apis.getUserInfo({access_token:''})
                    await this.$store.commit("SET_USERINFO",user_data.data)
                     setTimeout(()=>{
                         uni.reLaunch({
                             url: '/pages/main/user'
                         })
                    },1000)
                }

            },
            getCashList(){
                this.$router.push({
                    route:this.$routesConfig.getCashList,
                })
            },
            async getUserInfo(){
                let resData = await  this.$apis.getUserInfo({access_token:''})
                if(resData.code===0){
                    this.has_money=resData.data.price
                    this.mobile=resData.data.user_phone
                }
            },
            goToCard(){
                uni.navigateTo({
                    url: "/pages/user/bankCardList"
                })
            },
            handleMoney(){
                // let reg=/^\d+(?:\.\d{1,2})?$/
                // if(!reg.test(this.cashMoney)){
                //     this.can_send=false
                //     return
                // }
                //
                // this.$nextTick(function () {
                //     if(parseFloat(this.cashMoney)>this.has_money){
                //         this.cashMoney=this.has_money
                //     }
                //     let a=this.cashMoney.indexOf('.')
                //     if(a !== -1){
                //         let res=this.cashMoney.split('.')[1]
                //         if(res.length>2){
                //             this.cashMoney=String(parseFloat(this.cashMoney).toFixed(2))
                //         }
                //
                //     }
                //
                // })
                setTimeout(()=>{
                    // let reg=/^\d+(?:\.\d{1,2})?$/
                    // if(!reg.test(this.cashMoney)){
                    //     this.cashMoney=''
                    //     uni.showToast({
                    //         title:"请输入正确的提现金额",
                    //         icon: 'none',
                    //         duration:2000
                    //     })
                    //     return
                    // }
                    if(parseFloat(this.cashMoney)>this.has_money){
                        this.cashMoney=this.has_money
                    }
                    let a=this.cashMoney.indexOf('.')
                    if(a !== -1){
                        let data=this.cashMoney.split('.')
                        if(data[1].length>2){
                            // this.cashMoney=String(parseFloat(this.cashMoney).toFixed(2))
                            this.cashMoney=data[0] +'.' + data[1].substr(0,2)
                        }
                    }

                },100)

                // this.can_send=true
            },
            handleMoney1(){
                if(parseFloat(this.cashMoney)<100){
                    uni.showToast({
                        title:"请输入大于100提现金额",
                        icon: 'none',
                        duration:3000
                    })
                    return
                }
                let reg=/^\d+(?:\.\d{1,2})?$/
                if(!reg.test(this.cashMoney)){
                    uni.showToast({
                        title:"请输入最多两位小数的提现金额",
                        icon: 'none',
                        duration:3000
                    })
                    return
                }
            }

        },
    }
</script>
<style>
    page {
        background-color:#FAFAFA
    }
</style>
<style lang="less" scoped>
    .tixian_text{
        margin-top: 10rpx;
        font-size: 32rpx;
        color: red;
        text-align: center;
    }
    .getCash{
        background-color: #ffffff;
        margin-top: 2rpx;
    }
    .cash_box_1{
        display: flex;
        flex-direction: row;
        justify-content:space-between;
        margin-left: 20rpx;
        /*margin-top: 10rpx;*/
        padding-top: 30rpx;
    }
    .cash_box_1_1{
        display: flex;
        flex-direction: row;
        margin-right: 20rpx;
        /*margin-top: 10rpx;*/
        /*padding-top: 30rpx;*/
    }
    .small_img{
        width: 30rpx;
        height: 30rpx;
        transform: translate(0,2%);
    }
    .small_class_text{
        font-size: 29rpx;
        font-family: SourceHanSansCN-Normal;
        color: #333333;
    }
    .cash_box_2{
        display: flex;
        flex-direction: row;
        font-size: 50rpx;
        margin-left: 20rpx;
        margin-top: 30rpx;
    }
    .money_img{
        border-right: solid 0.5px #cccccc;
    }
    .money_num{
        padding-top: 10rpx;
    }
    .cash_box_3{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        margin-left: 20rpx;
        margin-top: 30rpx;
        padding-bottom: 30rpx;
    }
    .vis_money{
        font-size: 29rpx;
        color: #666666;
    }
    .all_money{
        font-size: 29rpx;
        color: #FF9400;
        margin-right: 20rpx;
    }
    .all_money_1{
        font-size: 28rpx;
        color: #FF9400;
        margin-right: 20rpx;
    }
    .cash_button{
        color: #ffffff;
        background-color: #FF9502;
        border-radius: 50rpx;
        width: 80%;
        margin-top: 300rpx;
    }
    .small_img_box{
        margin-left: 10rpx;
    }
    .choose_bank_box{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        padding-top: 20rpx;
        padding-bottom: 20rpx;
        padding-left: 20rpx;
        padding-right: 20rpx;
        background-color: #FAFAFA;
    }
    .bank_logo_box{
        display: flex;
        flex-direction: row;
        justify-content: left;
    }
    .bank_logo{
        width: 30rpx;
        height: 30rpx;
    }
    .arrow_img{
        width: 30rpx;
        height: 30rpx;
    }
    .choose_text{
        font-size: 28rpx;
        color: #333333;
        padding-top: 20rpx;
    }
    .bank_name{
        font-size: 28rpx;
        color: #333333;
        padding-left: 10rpx;
    }
    .bank_num{
        font-size: 28rpx;
        color: #333333;
        padding-top: 10rpx;
    }
    .arrow_box{
        padding-top: 20rpx;
    }
   .money_num_1{
       font-size: 28rpx;
       margin-left: 20rpx;
       padding-bottom: 10rpx;
   }
    .f-btn{
        box-sizing: border-box;
        width: 200rpx;
        height: 60rpx;
        line-height: 60rpx;
        align-self: center;
        color: #FF9400;
        font-size: 26rpx;
        padding: 0rpx 20rpx;
        border-color: #ffffff !important;
        background-color: #ffffff;
        /*background:linear-gradient(to top,#FF9502,#FF9502);*/
    }

</style>
